package com.alaingeorges.store.actions;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.joda.time.DateTime;

import com.alaingeorges.store.domain.Product;
import com.alaingeorges.store.domain.ProductCategory;
import com.alaingeorges.store.domain.ShoppingBasket;
import com.alaingeorges.store.domain.Supplier;
import com.alaingeorges.store.domain.User;



public class Action {

	public static void main(String[] args) {
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();

				createData(em);
		
		displayQueries(em);	

		em.getTransaction().commit();
		em.close();
		emf.close();




	}

	

	private static void displayQueries(EntityManager em) {
		Query q1 = em.createQuery("SELECT p FROM Product p WHERE p.name = 'Trip to Brazil' ");
//		Query q2 = em.createQuery("select man from Man man join man.dogs d where d.color='pink'");
//		Query q3 = em.createQuery("select h from House h join h.residents r where r.firstName='super'");
//		Query q4 = em.createQuery("select c from Man m join m.sharedCar c where m.lastName='man'");

		List<?> r1 = q1.getResultList();
//		List<?> r2 = q2.getResultList();
//		List<?> r3 = q3.getResultList();
//		List<?> r4 = q4.getResultList();


		System.out.println(r1);
//		System.out.println(r2);
//		System.out.println(r3);
//		System.out.println(r4);


	}




	private static void createData(EntityManager em) {
		
		Supplier toshiba = new Supplier("Toshiba", "Rue Haute", "23a", "1050 Bruxelles", "Belgium");
		Supplier apple = new Supplier("Apple", "Rue de l'Atomium", "1", "1220 Bruxelles", "Belgium");
		Supplier compaq = new Supplier("Compaq", "Rue Américaine", "25", "1060 Bruxelles", "Belgium");
		Supplier lenovo = new Supplier("Lenovo", "Grande Muraille", "259867455", "15412 Pekin", "China");
		Supplier motorola = new Supplier("Motorola", "Wall Street", "463", "Box 3 New York", "USA");
		Supplier sony = new Supplier("Sony", "YoyoStreet", "1", "6580 Tokyo", "Japan");
		Supplier sanoma = new Supplier("Sanoma", "Grand Rue", "3b", "1000 Bruxelles", "Belgium");
		Supplier condenast = new Supplier("Cone Nast", "High Street", "6", "45887ZR Los Angeles", "USA");
		Supplier panasonic = new Supplier("Panasonic", "Okinawa avenue", "12", "ZE52 Hiroshima", "Japan");
		Supplier folio = new Supplier("Folio", "Champs Elysees", "15", "Paris 16", "France");
		Supplier penguin = new Supplier("Penguin Books", "Regent St", "12", "London 25H M3", "UK");
		
		
		ProductCategory electronics = new ProductCategory();
		Set<ProductCategory> subElectronics = new HashSet<ProductCategory>();
		
		ProductCategory computers = null;
		Set<ProductCategory> subComputers = new HashSet<ProductCategory>();
		ProductCategory desktops = null;
		ProductCategory laptops = null;
		ProductCategory netbooks = null;
		ProductCategory smartphones = null;
		ProductCategory cameras = null;
		ProductCategory softwares = null;
		subElectronics.add(cameras);
		subElectronics.add(softwares);
		subElectronics.add(smartphones);
		subElectronics.add(computers);
		
		subComputers.add(desktops);
		subComputers.add(laptops);		
		subComputers.add(netbooks);
		ProductCategory cultural = null;
		Set<ProductCategory> subCultural = new HashSet<ProductCategory>();
		ProductCategory books = null;
		ProductCategory magazines = null;
		ProductCategory musicalAlbums = null;
		Set<ProductCategory> subMusicalAlbums = new HashSet<ProductCategory>();
		ProductCategory cds = null;
		ProductCategory k7s = null;
		ProductCategory lps = null;
		subCultural.add(magazines);
		subCultural.add(books);
		subCultural.add(musicalAlbums);
		
		subMusicalAlbums.add(k7s);
		subMusicalAlbums.add(cds);
		subMusicalAlbums.add(lps);
		
		Product thrillerCd = new Product("Thriller", 18.50, "5412368745598", cds, sony);
		Product thrillerLp = new Product("Thriller", 16.50, "5412368874555", lps, sony);
		Product thrillerK7 = new Product("Thriller", 12.50, "5412633587446", k7s, sony);
		Product apertureCd = new Product("Aperture", 120.00, "5630255879991", cds, apple);
		Product zoomUltraP = new Product("Zoom Ultra", 263.15, "6524158974555", cameras, panasonic);
		Product zoomUltraS = new Product("Zoom Ultra", 296.45, "3256698745882", cameras, sony);
		Product teleMoustique = new Product("Tele Moustique", 2.0, "54122365478895", magazines, sanoma);
		Product wired = new Product("Wired", 6.70, "65874523654788", magazines, condenast);
		Product iPhone6 = new Product("iPhone 6", 2000, "5413655897455", smartphones, apple);
		Product iPhone5s = new Product("iPhone 5s", 700, "563218974551", smartphones, apple);
		Product motoX = new Product("Moto X", 400, "658746322159", smartphones, lenovo);
		Product xperiaL = new Product("Xperia L", 180, "635897423556", smartphones, sony);
		Product macbook = new Product("MacBook Pro", 1600, "63578956411", laptops, apple);
		Product imac21 = new Product("iMac 21", 1700, "5478995423365", desktops, apple);
		Product sattelite25 = new Product("Satellite 25", 950, "63548977425", laptops, toshiba);
		Product satellire15 = new Product("Satellite 15", 315.25, "547889744582", netbooks, toshiba);
		Product voyage = new Product("Voyage au Brésil", 17.50, "5478992564412", books, folio);
		Product travel = new Product("Trip to Brazil", 17.80, "56879965412558", books, penguin);
		
		DateTime dtag = new DateTime(1965, 7, 21, 0,0,0,0);
		User alge = new User("Alain Georges", "Av Huart Hamoir", "52", "1030 Schaerbeek", "Belgium", dtag);
		DateTime dtab = new DateTime(1973, 4,16,0,0,0,0);
		User agbe = new User("Agnez Bewer", "Rue Thieffry", "26", "1030 Schaerbeek", "Belgium", dtab);
		DateTime dtfs = new DateTime(1975,2,28,0,0,0,0);
		User frse = new User("Frederic Servais", "Ch de Waterloo", "25", "1060 St Gilles", "Belgium", dtfs);
		DateTime dtsj = new DateTime(1950,5,30,0,0,0,0);
		User stjo = new User("Steve Jobs", "No Street", "5b", "365k Cupertino", "USA", dtsj);
		
		
		DateTime dtsbag = new DateTime();
		dtsbag.getDayOfYear();
		ShoppingBasket sbag = new ShoppingBasket(dtsbag, alge);
		sbag.setP(xperiaL);
		

	
		em.persist(toshiba);
		em.persist(apple);
		em.persist(compaq);
		em.persist(lenovo);
		em.persist(motorola);
		em.persist(sony);
		em.persist(sanoma);
		em.persist(folio);
		em.persist(condenast);
		em.persist(penguin);		
		em.persist(panasonic);



		em.persist(electronics);
		em.persist(subElectronics);
		em.persist(subComputers);
		em.persist(computers);
		em.persist(desktops);
		em.persist(laptops);
		em.persist(netbooks);
		em.persist(cameras);
		em.persist(smartphones);
		em.persist(softwares);
		em.persist(cultural);
		em.persist(subCultural);
		em.persist(magazines);

		em.persist(books);
		em.persist(musicalAlbums);
		em.persist(subMusicalAlbums);
		em.persist(cds);
		em.persist(lps);
		em.persist(k7s);
		
		em.persist(thrillerCd);
		em.persist(thrillerK7);
		em.persist(thrillerLp);
		em.persist(apertureCd);
		em.persist(zoomUltraP);
		em.persist(zoomUltraS);
		em.persist(teleMoustique);
		em.persist(wired);
		em.persist(iPhone5s);
		em.persist(iPhone6);
		em.persist(motoX);
		em.persist(xperiaL);
		em.persist(macbook);
		em.persist(imac21);
		em.persist(satellire15);
		em.persist(sattelite25);
		em.persist(voyage);
		em.persist(travel);
		em.persist(alge);
		em.persist(agbe);
		em.persist(frse);
		em.persist(stjo);
		
		
		em.persist(sbag);


		
		
		
		





	

	}

}
